package sa4

import scala.collection.mutable.HashMap
import scala.xml._

// class form, it takes the method of the form (as string) and a handler (as string)
class Form(method: String, handler: String) {

  var inputHashMap = new HashMap[String, (String, String, String)]

  //method to create the form
  def makeForm() = {
    var form = "<form " + "method=\"" + method + "\" handler=\"" + handler + "\" >"
    for ((key, value) <- inputHashMap) {
      var i = new Input(value._1, value._2, value._3)
      form = form + " " + i.makeInput 
    }
    form += "</form>"
    XML.loadString(form)
  }
  
  def testForm() : String = {
    var form = "<form " + "method=\"" + method + "\" handler=\"" + handler + "\" >"
    for ((key, value) <- inputHashMap) {
      var i = new Input(value._1, value._2, value._3)
      form = form + " " + i.makeInput 
    }
    form += "</form>"
    return form
  }
  
  //method to add input in the form
  def addInput(comment: String, inputType: String, name: String) {
    val input = new Input(comment, inputType, name)
    inputHashMap.put(name, (comment, inputType, name))
    makeForm
  }
  //method to remove an input from the form
  def removeInput(name: String) {
    if (inputHashMap.contains(name)) inputHashMap.remove(name)
    makeForm
  }

}
// class input, it takes the type of the input (as String) and the name or value of the input (as String)
class Input(comment: String, inputType: String, name: String) {

  //method to create the input
  def makeInput() = {
    var input = "<p>" +comment + " <input type=\"" + inputType + "\""
    if (!(inputType.equals("submit") || inputType.equals("radio"))) input += " name=\"" + name + "\" /> </p>" else input += " value=\"" + name + "\" /> </p>"
    XML.loadString(input)
  }

}